GetUnit Function

public function GetUnit() result(iunit)

returns a free FORTRAN unit number Discussion: A "free" FORTRAN unit number is an integer between 1 and 999 which is not currently associated with an I/O device. A free FORTRAN unit number is needed in order to open a file with the OPEN command. If IUNIT = 0, then no free FORTRAN unit could be found, although all 999 units were checked (except for units 5 and 6). Otherwise, IUNIT is an integer between 1 and 99, representing a free FORTRAN unit. Note that GetUnit assumes that units 5 and 6 are special, and will never return those values. Adapted from John Burkardt

Arguments

None

Return Value integer(kind=short)


Variables

Type Visibility Attributes Name Initial
integer(kind=short), public :: i
integer(kind=short), public :: ios
logical, public :: lopen

Source Code

FUNCTION GetUnit () &
RESULT (iunit)

IMPLICIT NONE

! Local scalars:
INTEGER (KIND = short)     :: iunit 
INTEGER (KIND = short)     :: i
INTEGER (KIND = short)     :: ios  
LOGICAL                    :: lopen
!------------end of declaration------------------------------------------------
  iunit = 0

  DO i = 1, 999
    IF ( i /= 5 .AND. i /= 6 ) THEN
      INQUIRE ( unit = i, opened = lopen, iostat = ios )
      IF ( ios == 0 ) THEN
        IF ( .NOT. lopen ) THEN
          iunit = i
          RETURN
        END IF
      END IF
    END IF
  END DO

  RETURN
END	FUNCTION GetUnit